package com.txw.travel.controller.backstage;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.txw.travel.pojo.Category;
import com.txw.travel.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/backstage/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer size) {
        ModelAndView modelAndView = new ModelAndView();
        Page<Category> categoryPage = categoryService.getCategoryList(page, size);
        modelAndView.addObject("categoryPage",categoryPage);
        modelAndView.setViewName("/backstage/category_all");
        return modelAndView;
    }

    @RequestMapping("/add")
    public String add(Category category) {
        categoryService.insertCategory(category);
        return "redirect:/backstage/category/all";
    }

    @RequestMapping("/edit")
    public ModelAndView edit(Integer cid) {
        ModelAndView modelAndView = new ModelAndView();
        Category category = categoryService.getCategoryById(cid);
        modelAndView.addObject("category",category);
        modelAndView.setViewName("/backstage/category_edit");
        return modelAndView;
    }

    @RequestMapping("/update")
    public String update(Category category) {
        categoryService.updateCategory(category);
        return "redirect:/backstage/category/all";
    }

    @RequestMapping("/delete")
    public String delete(Integer cid) {
        categoryService.deleteCategory(cid);
        return "redirect:/backstage/category/all";
    }
}
